<?php

namespace App\Console\Commands;


use App\Helpers\Curl;
use App\Helpers\DateUtils;
use App\Helpers\Logger\SLogger;
use App\Models\Orm\CoinMarket;
use Illuminate\Console\Command;
use DB;

class GetCoin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'GetCoin';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '主动发起查询事件';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $urlConfig = "https://api.coinmarketcap.com/v1/ticker/?limit=1535";

        $datas = Curl::curl($urlConfig);

        $datas = json_decode($datas,true);

        if(!empty($datas))
        {
            foreach ($datas as $data) {


                DB::beginTransaction();
                try {
                    $data['nid'] = $data['id'];
                    unset($data['id']);
                CoinMarket::firstOrCreate(['nid' => $data['nid']],$data);

                    DB::commit();
                } catch (\Exception $e) {

                    SLogger::getStream()->info("数据导入失败");
                    SLogger::getStream()->info($e);
                    DB::rollBack();
                }
            }
        }


    }
}
